<template>
	<view>
		<cu-custom bgColor="bg-gradual-red" :isBack="true">
			<block slot="content">新增地址</block>
		</cu-custom>
		  <view class="cu-form-group margin-top">
						<view class="title" >收货人</view>
						<input  name="input" v-model="address.shippingUser"></input>
					</view>
					
						<!-- #ifndef H5 || APP-PLUS || MP-ALIPAY -->
						<view class="cu-form-group">
							<view class="title">地址选择</view>
							<picker mode="region" @change="RegionChange" :value="region">
								<view class="picker">
									{{region[0]}}，{{region[1]}}，{{region[2]}}
								</view>
							</picker>
						</view>
						<!-- #endif -->
					
					<view class="cu-form-group">
						<view class="title">详细地址</view>
						<input   v-model="address.address" name="input"></input>
						<text class='cuIcon-locationfill text-orange'></text>
					</view>
					
					<view class="cu-form-group">
						<view class="title">验证码</view>
						<input placeholder="输入框带个按钮" name="input"></input>
						<button class='cu-btn bg-green shadow'>验证码</button>
					</view>
					<view class="cu-form-group">
						<view class="title">手机号码</view>
						<input   v-model="address.shippingPhone" name="input"></input>
						<view class="cu-capsule radius">
							<view class='cu-tag bg-blue '>
								+86
							</view>
							<view class="cu-tag line-blue">
								中国大陆
							</view>
						</view>
					</view>
					
				
					
				<view class="cu-form-group">
					<button class='cu-btn bg-green shadow'   @click="saveAddress">保存地址且返回</button>
				</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address:{
					  address: '',
					  city: '',
					  customerId: 0,
					  district: '',
					  id: 0,
					  isDefault: 0,
					  modifiedTime:'',
					  province: '',
					  shippingPhone: '',
					  shippingUser: ''
					
				},
				region: ['广东省', '广州市', '海珠区']
			}
		},
		methods: {
			saveAddress:function(){
				//表单验证
				if(!this.address.shippingUser){
					uni.showToast({
						title:'收货人不能为空',
						duration:1000
					})
					return ;
				}
				if(!this.address.shippingPhone){
					uni.showToast({
						title:'联系方式不能为空',
						duration:1000
					})
					return ;
				}
				
			
				//获取顾客的地址
				this.address.customerId = this.$global.userinfo.id;	
				this.address.province  = this.region[0];
				this.address.city =this.region[1];
				this.address.district =this.region[2];
				this.$api.__api__addAddr(this.address).then(res =>{
					uni.showToast({
						title:'地址已经保存',
						duration:2000
					})
					//触发adress事件告诉它 我回来且把新地址带回去
					uni.$emit("address");   //通知我回来 但是不要数据
					uni.navigateBack({
						delta:1
					});
					
					
				})
				
				
				
			},
			RegionChange(e) {
				this.region = e.detail.value
			}
		}
	}
</script>

<style>

</style>
